import EngineHelper from "../EngineHelper";
import UIManager from "../../cfw/ui/UIManager";
import { isNull } from "../../cfw/tools/Define";
import BaseItemModel from "../../cfw/mvc/BaseItemModel";
enum Guide_infoEnum{
    guideID,
    posInfo,
    endInfo,
    des,
    fingerDir,
    descDir,
    isForce,
    hasMask,
}

export class WidgetInfo {

    private className: string
    private widgetName: string;
    private itemID: any;
    private widget: cc.Node;
    constructor(s: string) {
        let list = s.split(':')
        this.className = list[0]
        this.widgetName = list[1]
        if (list.length >= 3) {
            this.itemID = list[2]
        }

        // cc.log(' this ============ widgetInfo ', this)
    }


    check(layer: cc.Node, id?: any) {
        let className = layer.name;
        if (className == this.className) {
            return true;
        }
    }

    setWidget(layer: cc.Node, id?: any) {
        let className = layer.name;
        if (this.className != className) {
            return;
        }
        if (id) {
            if (this.itemID == id) {
                if (this.widgetName == 'this') {
                    this.widget = layer
                } else {
                    this.widget = EngineHelper.findChild(this.widgetName,UIManager.instance().getRoot())
                }
            }
        } else {
            if (this.widgetName == 'this') {
                this.widget = layer
            } else {
                this.widget = EngineHelper.findChild(this.widgetName,layer)
            }
        }
    }

    isOK() {
        return !isNull(this.widget)
    }

    getWidget() {
        return this.widget;
    }

    getClassName() {
        return this.className
    }

    getWidgetName() {
        return this.widgetName;
    }

    getItemID() {
        return this.itemID;
    }
}

export default class GuideItemModel extends BaseItemModel {



    private data: any[]
    private widgetInfo: WidgetInfo[] = []

    private guideID: number;
    constructor(id, data) {
        super()
        this.setID(id)
        this.data = data;
        this.guideID = this.data[Guide_infoEnum.guideID]
        let posInfo: any[] = this.data[Guide_infoEnum.posInfo];
        for (let index = 0; index < posInfo.length; index++) {
            const element = posInfo[index];
            this.widgetInfo[index] = new WidgetInfo(element)
        }


    }

    hasMask() {
        return this.data[Guide_infoEnum.hasMask] == 1
    }


    getGuideID() {
        return this.guideID;
    }


    getPos(index: number) {
        if (index < this.widgetInfo.length) {
            return this.widgetInfo[index].getWidget();
        }
        return null;

    }



    isFull() {
        for (let index = 0; index < this.widgetInfo.length; index++) {
            const element = this.widgetInfo[index];
            if (!element.isOK()) {
                return false;
            }

        }
        return true;
    }

    checkNode(layer: cc.Node, id?: any) {
        // let className = layer.name;
        // let index = this.widgetInfo.length;
        for (let index = 0; index < this.widgetInfo.length; index++) {
            const element = this.widgetInfo[index];
            if (!element.isOK()) {
                element.setWidget(layer, id)
                break;
            }
        }

    }

 

    getEndInfo() {
        return this.data[Guide_infoEnum.endInfo]
    }

    getDesc() {
        return this.data[Guide_infoEnum.des]
    }

    getFingerDir() {
        return this.data[Guide_infoEnum.fingerDir]
    }

    getDescDir() {
        return this.data[Guide_infoEnum.descDir]
    }

    isForce() {
        return this.data[Guide_infoEnum.isForce] == 1
    }

    check(layerName: string, funcName: string, id?: any) {

        cc.log(' layerName ', layerName, ' funcName ', funcName)
        let endInfo = this.getEndInfo()
        cc.log(' endInfo ', endInfo)
        let endList: string[] = endInfo.split(':')
        if (id) {
            return endList[0] == layerName && endList[1] == funcName && endList[2] == id;
        } else {
            return endList[0] == layerName && endList[1] == funcName
        }

    }

}
